package com.otto.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.otto.reggie.common.CustomException;
import com.otto.reggie.common.R;
import com.otto.reggie.entity.Category;
import com.otto.reggie.entity.Dish;
import com.otto.reggie.entity.Setmeal;
import com.otto.reggie.service.CategoryService;
import com.otto.reggie.service.DishService;
import com.otto.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者：otto
 * 时间：2022-08-11 19:39:39
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 保存分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("收到前端传来参数: 分类信息 ——> {}", category.toString());
        return categoryService.saveCategory(category);
    }

    /**
     * 分类信息分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        log.info("收到前端传来参数: 分页页码 ——> {}，每页条数 ——> {}", page, pageSize);
        Page pageInfo = new Page(page, pageSize);
        return R.success(categoryService.page(pageInfo));
    }

    /**
     * 根据id删除分类（如果分类关联套餐或者菜品则不可删除）
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam(name = "ids") Long id) {
        log.info("收到前端传来参数: 分类id ——> {}", id);
        //检查Dish中有没有存在该分类的
        LambdaQueryWrapper<Dish> dishQueryWrapper =
                new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(dishQueryWrapper);
        //如果有，则不能删除
        if (count > 0) {
            throw new CustomException("该分类有关联的菜品，不能删除!");
            //return R.error("该分类有关联的菜品，不能删除!");
        }
        //检查Setmeal中有没有存在该分类的
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper =
                new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
        count = setmealService.count(setmealQueryWrapper);
        //如果有，则不能删除
        if (count > 0) {
            throw new CustomException("该分类有关联的套餐，不能删除!");
            //return R.error("该分类有关联的套餐，不能删除!");
        }
        //否则，可以删除
        categoryService.removeById(id);
        return R.success("分类信息删除成功");
    }

    /**
     * 根据id修该分类信息
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("更新分类信息成功");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        log.info("CategoryController.list方法收到参数 {}",category.toString());
        Integer type = category.getType();
        LambdaQueryWrapper<Category> categoryQueryWrapper = new LambdaQueryWrapper<>();
        categoryQueryWrapper.eq(type != null, Category::getType, type);
        categoryQueryWrapper.orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(categoryQueryWrapper);
        return R.success(list);
    }
}
